package com.ruoyi.web.init;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import com.github.pagehelper.util.StringUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.enums.BaseEnum;
import com.ruoyi.common.enums.WorkflowStatus;
import com.ruoyi.lc.enums.LcDatamodelColumnMappingTypeEnum;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;

@Component
public class DictInitCommandLineRunner implements CommandLineRunner {

	@Autowired
	private ISysDictTypeService dictTypeService;

	@Autowired
	private ISysDictDataService dictDataService;

	@Override
	public void run(String... args) throws Exception {
		List<BaseEnum> enums = new ArrayList<BaseEnum>();
		enums.addAll(Arrays.asList(LcDatamodelColumnMappingTypeEnum.values())); //添加需要初始化的枚举
		enums.addAll(Arrays.asList(WorkflowStatus.values())); //添加需要初始化的枚举
		if (enums != null) {
			for (BaseEnum e : enums) {
				SysDictType type = dictTypeService.selectDictTypeByType(e.getType());
				if (type == null) {
					type = new SysDictType();
					type.setDictName(e.getTypeName());
					type.setDictType(e.getType());
					type.setCreateTime(new Date());
					type.setStatus("0");
					dictTypeService.insertDictType(type);
				}
				String d = dictDataService.selectDictLabel(type.getDictType(), e.getCode());
				if (StringUtil.isEmpty(d)) {
					SysDictData data = new SysDictData();
					data.setCreateTime(new Date());
					data.setDictValue(e.getCode());
					data.setDictLabel(e.getInfo());
					data.setDictType(e.getType());
					data.setStatus("0");
					dictDataService.insertDictData(data);
				}
			}

		}

	}
}
